<template>
  <div class="home">
    <el-card>
      <div slot="header" class="clearfix">
        <el-button @click="editAdvertSpace()">添加广告位</el-button>
      </div>
      <el-table
        :data="spaces"
        style="width: 100%">
        <el-table-column
          prop="id"
          align="center"
          label="spaceKey"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="广告位名称">
        </el-table-column>
        <el-table-column
          align="center"
          label="创建时间">
          <template slot-scope="scope">
            {{ getTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          align="center"
          label="更新时间">
          <template slot-scope="scope">
          {{ getTime(scope.row.updateTime) }}
        </template>
        </el-table-column>
        <el-table-column
          prop="address"
          align="center"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button type="info" @click="editAdvertSpace(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        :title="isEdit ? '编辑广告位' : '添加广告位'"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <!-- 广告位名称：
        <el-input
          placeholder="请选择日期"
          v-model="formSpace.name">
        </el-input> -->
        <el-form
          ref="form"
          :model="formSpace"
          :rules="rules"
          label-width="100px"
          label-position="right"
        >
          <el-form-item prop="name" label="广告位名称:">
            <el-input v-model="formSpace.name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAdSpacesList, getSpaceById, saveOrUpdateSpace } from '@/services/advert'
import { getTime } from '@/utils/time'

export default Vue.extend({
  name: 'AdvertSpace',
  created () {
    this.LoadAdSpacesList()
  },
  data () {
    return {
      spaces: [],
      form: {
        pageNum: 1,
        pageSize: 5
      },
      formSpace: {
        name: ''
      },
      allSpaces: [],
      isEdit: false,
      rules: {
        name: [
          { required: true, message: '请输入资源名称', trigger: 'blur' }
        ]
      },
      dialogVisible: false
    }
  },
  methods: {
    async LoadAdSpacesList () {
      const { data } = await getAdSpacesList(this.form)
      this.spaces = data.content
    },
    getTime (time: string | number) {
      return getTime(time)
    },
    async editAdvertSpace (item: any) {
      if (item && item.id) {
        const { data } = await getSpaceById({ id: item.id })
        this.formSpace = data.content
        this.isEdit = true
      } else {
        this.formSpace = { name: '' }
        this.isEdit = false
      }
      this.dialogVisible = true
    },
    async onSubmit () {
      await saveOrUpdateSpace(this.formSpace)
      this.dialogVisible = false
      this.$message.success('保存成功')
      this.LoadAdSpacesList()
    }
  }
})
</script>

<style lang="scss" scoped></style>
